<?php

//require "../runReplace.php";
require "../framework.php";

global $pageTable;
$table=$pageTable;

if (isset($_REQUEST['table']))
    $table=$_REQUEST['table'];

$field="text";

if (isset($_REQUEST['field']))
    $field=$_REQUEST['field'];

$title="";

if (isset($_REQUEST['title']))
    $title=$_REQUEST['title'];

$data=new data();
$data->getQuery("SELECT * FROM $table WHERE id2='" . $_REQUEST['id2'] . "'");
//$data = sqlQuery("SELECT * FROM $table WHERE id2='".$_REQUEST['id2']."'");

$text                        =$data->data[$field];
$id2                         =$data->data['id2'];
$url                         =$data->data['_url'];
$title                       =$data->data['titel'];
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Ajax Edit</title>

        <link href = "ajaxEdit.css" rel = "stylesheet" type = "text/css">
        <link href = "ajax_css.php" rel = "stylesheet" type = "text/css">
        <link href = "/snapol/admin_template/admin.css" rel = "stylesheet" type = "text/css">
        <script src = "/api/jquery/jquery.js"></script>
        <script src = "/api/jquery/jquery-ui.js"></script>

        <link href = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel = "stylesheet"
              type = "text/css"/>

        <style type = "text/css">
            .ui-state-highlight
                {
                height:      1.5em;
                line-height: 1.2em;
                background-color: gray;
                }

            .snapol_property
                {
                position: relative;
                left: 0px;
                top:  0px;
                z-index: 1;
                top:  16px;
                background-color: white;
                width: 50px;
                height: 16px;
                }
                
            form {
                background-color: gray;
                
                
            }
                
                
                
        </style>

        <script>
        $(document).ready(function() {
    $("#accordion").accordion( {active:false, autoHeight:false});
    
     preparePage();
    

    
     // sortable
     $("#editor, #editor form").sortable({ connectWith: 'form' });
     
     // resize elmenten (for instance image)
     //$("#editor").resizable();  --> muss wieder gelöscht werden

     
     //$( "#editor input" ).draggable();     
     
     // forms 
   /*  $("#editor form").droppable({
          drop: function(event, ui) { 
            
           // alert(ui.draggable.html()); 
            html = ui.draggable.html();
            //id = ui.draggable.attr("id");
            id = ui.draggable.attr('id')
            //alert(id);
            //alert(html);
            
            //$("#"+id).remove();
            //$(this).append(html);
            
            
            
          }
        }); */
        
        
        

     
     
     
     
     
     
     /*$("#editor").sortable({ revert: true , helper: 'clone',cursor: 'crosshair' ,placeholder: 'ui-state-highlight', change: function(event, ui) { 
         
      
       $("#property_" + counter).append("<p>hello world</p>"); 
         
         $("#anzeige").html("anzeige "+$(this).offset.top);
         
         
         }
     });  */
    
    
  });
        </script>

        <script type = "text/javascript" src = "ajaxEdit.js"></script>
        <!--<script type="text/javascript" src="jQueryImageResize.js"></script>    
        <script type="text/javascript" src="jQueryContextmenu.js"></script>  
        <!-- http://ditio.net/2010/01/02/jquery-image-resize-plugin/ -->

        <script type = "text/javascript" src = "/snapol/ajaxEdit/wizzard.js"></script>

        <script type = "text/javascript" src = "/snapol/js/ajax_Data.js"></script>

        <!-- damit es keinen fehler beim wizzard gibt -->
        <!--<script src="/snapol/js/ajax_Edit.js"></script>-->

        <script type = "text/javascript">
        function deleteItem(id, propertyId)
                {
                
                  //  alert(propertyId);
                $('#wrap_' + id).remove();
                //$('#' + propertyId).remove();
                //$('.snapol_property').remove();
                
                
                
                //preparePage(); 
                
                }

                
                
                
                
            function showProperty(id, com)
                {

                // html = $(this).attr('id')+'  '+ $(this).get(0).tagName;   
                
                //falls Link
                /*if (com = "A") {
                    
                } */
                
                //alert(com);  
                
                
                // falls table Objekt
                if (com == "TABLE") {
                    
                    
                    
                    
                     url = "/snapol/ajaxEdit/getTableProperty.php?com=" + com; //$(this).get(0).tagName;
                $("#someDiv").load(url, function(data) {
                          
                    
               /*     for (n=0; n<propertyCount; n++) {       
                     value = $("#"+id).attr(propertyList[n]);    
                      $("#property_"+propertyList[n]).attr("value", value);    
                    } */
                      
}).dialog({ modal: true, title: 'Property ' + com, open: function(event, ui) {
                
                
                }, buttons: { "Ok": function()
                    {
                    
                                   
                 /*   for (n=0; n<propertyCount; n++) {
                      
                      value = $("#property_"+propertyList[n]).attr("value"); 
                      $("#"+id).attr(propertyList[n], value); 

                         
                    }  */
                    
                    
                    
                    
                    $(this).dialog("close");
                    }
                }});
                    
                } else {
                    
                    
                    
                    // falls input
                    
                   /* if (com == "INPUT") {
                        
                        com = 
                        
                    }  */
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
                url = "/snapol/ajaxEdit/getProperty.php?com=" + com; //$(this).get(0).tagName;
                $("#someDiv").load(url, function(data) {
  
                    
                    //alert("anzahl "+propertyCount);
                    //alert(data);
                    
                    //alert('Load was performed.');
                    
                    // get List of com
                    //comProperty = 
                    
                    
                    for (n=0; n<propertyCount; n++) {
                            value = $("#"+id).attr(propertyList[n]);    
                            $("#property_"+propertyList[n]).attr("value", value);    
                    }
                    
                    //value = $("#"+id).attr("strasse");    
                    //alert(value);
                    
                    //value = "asdf";
                    //$("#property_strasse").attr("value", value);    
  
}).dialog({ modal: true, title: 'Property ' + com, open: function(event, ui) {
                
                
                }, buttons: { "Ok": function()
                    {
                    
                    /*ort = $("#property_strasse").attr("value");
                    $("#" + id).html("hello ---------------------Ort = " + ort
                                         + "--------------------------------------------------------------------------------------------------------------- world");

                     $("#"+id).attr("strasse", ort); */   
                    
                    html = "<p><b>"+com+"</b></p>";
                    
                    for (n=0; n<propertyCount; n++) {
                      
                      value = $("#property_"+propertyList[n]).attr("value"); 
                      $("#"+id).attr(propertyList[n], value); 
                        
                        
                        html = html + "<b>"+propertyList[n]+"</b>: "+ $("#property_"+propertyList[n]).attr("value")+"<br>";

                        
                    }
                    
          // wieder aktivieren          $("#" + id).html(html);
                    
                    
                    
                    $(this).dialog("close");
                    }
                }});
                }
                
              //  alert("hello");
                
                //ort = $("#property_strasse").attr("value");    
                
                }
               

                        
          
                        
            function unwrapElement() {
                
                $('.snapol_property').remove();
          //      $('.wrap_property').unwrap();
                
                
                
                
                
                
            $("#editor form, #editor input, #editor a,#editor table,<?php $dataCom = new data(); $dataCom->getTable("_com"); do { echo "#editor {$dataCom->data['com']},"; } while($dataCom->nextRow());?>").unwrap();
           
        
                
            }            
            
            
        
       
        
        
            
                        
                        
function preparePage() {
    
    
     //$("#editor form, #editor input, #editor a,#editor table,<?php $dataCom = new data(); $dataCom->getTable("_com"); do { echo "#editor {$dataCom->data['com']},"; } while($dataCom->nextRow());?>").wrap('<div class="new" />edit');
     
         //#editor form, #editor input, #editor a,#editor table,
      $("<?php $dataCom = new data(); $dataCom->getTable("_com"); do { echo "#editor {$dataCom->data['com']},"; } while($dataCom->nextRow());?>").each(function(counter)
    
                    {
                       
                       
                        //$(this).wrap('<div class="snapol_property" id="\'property_' + counter + '\'">');    
                   //     $(this).wrap('<div class="wrap_property">');    
                        
                    });
     
    
    
       //#editor form, #editor input, #editor a,#editor table,
    $("<?php $dataCom = new data(); $dataCom->getTable("_com"); do { echo "#editor {$dataCom->data['com']},"; } while($dataCom->nextRow());?>").each(function(counter)
    
    
    //$("#editor table,#editor form, #editor input, #editor select, #editor a, #editor map_search, #editor mp3,").each(function(counter)
                    {

                    //alert("start");
                    //alert(a);

                    //alert(this.);
                    //$(this).css("background", "blue");
                    //$(this).css("border","3px solid red");
                    //this.style.color = "blue";

                    var offset = $(this).offset();
                    //alert( "left: " + offset.left + ", top: " + offset.top);
                    com = $(this).get(0).tagName;

                    // falls Link
                    if (com == "A") {
                        //alert("a element");
                        com = $(this).attr("href");
                        //alert(com);
                    }
                    
                    
                    // falls Form
                    if (com == "FORM") {
                        //alert("a element");
                        com = $(this).attr("action");
                        
                        if (com == "")
                            com = "FORM";
                        
                        //alert(com);
                    }
                    
                    
                    // falls Input
                    if (com == "INPUT") {
                        //alert("a element");
                        com = "input_"+$(this).attr("type");
                        //alert(com);
                    }
                    
                    
                    
                    
                    
                    
                    
                    id = $(this).attr("id");
                    
                    // id property setzen, falls nicht vorhanden
                    if (id == "") {
                        id = "counter_"+(counter+1);   // überprüfen, ob id bereits vorhanden ist
                        $(this).attr("id", id);     
                    }
                    
                    wrapElement(id, com);
                    
                    
                                                 /*  + '\'" style="position:absolute; background-color:#09F; width:100px; top:' + offset.top
                               + 'px; left:' + offset.left + 'px">';  */
                    
                 /*   html = '<div class="snapol_property" id="\'property_' + counter
                               + '\'" style="background-color:#09F; width:100px;">';
                    html = html + '<a href="#" onclick="deleteItem(\'' + id + '\', \'property_'
                               + counter + '\');return false;">Delete</a> ';
                    html = html + '<a href="#" onclick="showProperty(\'' + id + '\',\'' + com
                               + '\');return false;">Edit</a> ';
                    html = html + '</div>'; */
                   
                   
                   //    $(".wrap_property").append(html); 
                   
                   // $('html').append(html);

                    //alert(html);

                    //$(this).wrap('<div class="snapol_property" id="\'property_' + counter + '\'">');
                               
                    //$("#property_" + counter).append("<p>hello world</p>");
                    //html(html+$("#property_" + counter).html());           
                    
                    
                    
                    //alert(this.innerHTML);

                    });
                    
                     //alert($("#editor").html());               
    
    
}                        
                        
                    
                           
                  //alert("hello");
                

                //  $("#someDiv").dialog.load('snapol/ajaxEdit/getProperty.php')( {
                //$('.result').html(data);
                //alert('Load was performed.');

              //  }


            //  alert('/snapol/ajaxEdit/getProperty.php?com='+$(this).get(0).tagName);         


            // anfügen der edit/delete buttons
        </script>

        <link href = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel = "stylesheet"
              type = "text/css"/>
    </head>

    <body>
        <div id = "anzeige">
        </div>

        <h1><?php echo $title; ?></h1>

        <p></p><a href = "#" class = "snapol_button" onClick = "saveContent(); return false">save content</a>

        </p>

        <div id = "toolbar">
            <div id = "accordion">
                <h3><a href = "#">Wizzard</a></h3>

                <div>
                    <p id = "currentId">current id</p>

                    <a href = "#" onClick = "saveContent(); return false">save content</a>

                    <br>
                    <p>
                    <input type = "submit"                              name = "button7"
                           id = "button7"                               value = "Form Wizzard"
                           onclick = "showFormWizzard(); return false;" style = "width:150px"/> </p>

                    <p>
                    <input type = "submit"                               name = "button8"
                           id = "button8"                                value = "Table Wizzard"
                           onclick = "showTableWizzard(); return false;" style = "width:150px"/> </p>

                    <p>
                    <input type = "submit"                                name = "button9"
                           id = "button9"                                 value = "Detail Wizzard"
                           onclick = "showDetailWizzard(); return false;" style = "width:150px"/> </p>

                    <p>
                    <input type = "submit"                                  name = "button9"
                           id = "button9"                                   value = "Document"
                           onclick = "showDocumentWizzard(); return false;" style = "width:150px"/> </p>

                    <p>
                    <input type = "submit"                          name = "button9" id = "button9" value = "CMS Link"
                           onclick = "showCmsLink(); return false;" style = "width:150px"/> </p>

                    Eigenschaft

                    <table width = "100%" border = "0">
                        <tr>
                            <td>Url</td>

                            <td></td>
                        </tr>
                    </table>

                    <input id = "page_id2" type = "hidden" value = "<?php echo $id2;?>">
                    <input id = "page_url" type = "hidden" value = "<?php echo $url;?>"> Speichern/Close Wizzard
                </div>

                <?php
                $dataComGroup=new data();
                $dataComGroup->getTable("_com_group", "", "com_group");

                do
                    {

                    // sind in dieser comgroup componenten vorhanden    
                    $comCount=db::getTableCount("_com",
                                                "__com_group={$dataComGroup->data['id']} and _usergroup<={$userData['_usergroup']}");

                    if ($comCount > 0)
                        {
                ?>

                        <h3><a href = "#"><?php echo $dataComGroup->data['com_group']; ?></a></h3>

                        <div>
                            <ul class = "snapol_list" style = "padding-left: 0px;">
                        <?php
                                $dataCom=new data();
                                $dataCom->getTable("_com", "__com_group={$dataComGroup->data['id']}", "com");

                                do
                                    { //<img src="'.$dataCom->data['icon_32'].'" />
                                    echo '<li  style="padding-left: 0px;"><a href="#" onClick="insertCom(\''
                                             . $dataCom->data['com'] . '\',\'tag\'); return false;">'
                                             . $dataCom->data['doc_title'] . '</a></li>';
                                    /*echo '<li  style="padding-left: 0px;"><a href="#" onClick="insertCom(\'' . $dataCom->data['com'] . '\',\'command\'); return false;">'
                                             . $dataCom->data['com'] . ' (Command)</a></li>';   */

                                    } while ($dataCom->nextRow());
                        ?>
                            </ul>
                        </div>

                <?php
                        }
                    } while ($dataComGroup->nextRow());
                ?>
            </div>
        </div>

        <div id = "editor">
            <?php echo $text ?>
        </div>

        <!--     <div id = "property"><h3>Property</h3>
     
                 <table width = "100%" border = "0">
                     <tr>
                         <td>Tag</td>
     
                         <td>
                             <label><input type = "text" name = "p_tag" id = "p_tag"/></label></td>
                     </tr>
     
                     <tr>
                         <td>Name</td>
     
                         <td>
                             <label><input type = "text" name = "p_name" id = "p_name"/></label></td>
                     </tr>
     
                     <tr>
                         <td>ID</td>
     
                         <td>
                             <label><input type = "text" name = "p_id" id = "p_id"/></label></td>
                     </tr>
     
                     <tr>
                         <td>Value</td>
     
                         <td>
                             <label><input type = "text" name = "p_value" id = "p_value"/></label></td>
                     </tr>
     
                     <tr>
                         <td>Table</td>
     
                         <td>
                             <label><select name = "p_table" id = "p_table">
                                 <option value = "alpha">alpha</option>
     
                                 <option value = "news">news</option>
     
                                 <option value = "page">page</option>
                             </select>
     
                             </label></td>
                     </tr>
     
                     <tr>
                         <td>Order By</td>
     
                         <td>
                             <label><input type = "text" name = "p_orderby" id = "p_orderby"/></label></td>
                     </tr>
                 </table>
     
                 property list
     
                 <div id = "property2">
                 </div>
             </div>   -->

        <div id = "someDiv">
        </div>
    </body>
</html>